package sdk.webview.fmc.com.fmcsdk.record.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * =====================================
 * 作    者: 陈嘉桐
 * 版    本：1.1.4
 * 创建日期：2017/5/25
 * 描    述：
 * =====================================
 */
public class ScreenUtils {
    public static int getScreenHeight(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metric);
        return metric.heightPixels;
    }

    public static int getScreenWidth(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }

    /**
     * 获取状态栏高度
     */
    public static int getStatusBarHeight() {
        Resources resources = Resources.getSystem();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

    public static Point getStatusBarSize(Activity activity) {
        // 默认状态栏高度
        int statusBarHeight = 0;
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int id = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = activity.getResources().getDimensionPixelSize(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 获取状态栏的宽度
        // 注意：状态栏宽度通常是屏幕宽度，但有些设备可能会有nav bar（导航栏）
        Point size = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(size);
        return new Point(size.x, statusBarHeight); // 返回宽和高
    }

    public static float pxToDp(Context context, float px) {
        float density = context.getResources().getDisplayMetrics().density;
        return px / density;
    }

    public static float convertPxToCssPx(Context context, float px) {
        // 获取屏幕的密度
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        float density = displayMetrics.density;
        // 转换px到css px
        return px / density;
    }

    public static int getScreenDPI(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int dpi = displayMetrics.densityDpi;
        return dpi;
    }
}
